package com.example.zbh.dsliao.utils;

import android.preference.PreferenceManager;

import com.easemob.EMValueCallBack;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.SaveCallback;

import java.util.Map;

/**
 * 个人资料设置操作帮助类(parse)的查询 修改 保存到缓存等方法
 */
public class UserProfileManager {

    private static final String ParseAppID = "UUL8TxlHwKj7ZXEUr2brF3ydOxirCXdIj9LscvJs";
    private static final String ParseClientKey = "B1jH9bmxuYyTcpoFfpeVslhmLYsytWTxqYqKQhBJ";

    private static final String CONFIG_TABLE_NAME = "geanwen";
    private static final String CONFIG_USERNAME = "username";
    private static final String CONFIG_NICK = "nickname";
    private static final String CONFIG_AVATAR = "avatar";

    private static DlUser currentUser;
    private Map<String, DlUser> contactList;

    public static void asyncGetCurrentUserInfo() {
        asyncGetCurrentUserInfo(new EMValueCallBack<DlUser>() {

            @Override
            public void onSuccess(DlUser value) {
                if (value != null) {
                    setThreeCurrentUserNick(value.getNick());
                    setThreeCurrentUserAvatar(value.getAvatar());
                }
            }

            @Override
            public void onError(int error, String errorMsg) {

            }
        });

    }

    public static void asyncGetCurrentUserInfo(final EMValueCallBack<DlUser> callback){
        final String username = EMChatManager.getInstance().getCurrentUser();
        asyncGetUserInfo(username, new EMValueCallBack<DlUser>() {

            @Override
            public void onSuccess(DlUser value) {
                callback.onSuccess(value);
            }

            @Override
            public void onError(int error, String errorMsg) {
                if (error == ParseException.OBJECT_NOT_FOUND) {
                    ParseObject pUser = new ParseObject(CONFIG_TABLE_NAME);
                    pUser.put(CONFIG_USERNAME, username);
                    pUser.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException arg0) {
                            if (arg0 == null) {
                                callback.onSuccess(new DlUser(username));
                            }
                        }
                    });
                } else {
                    callback.onError(error, errorMsg);
                }
            }
        });
    }




    public static void asyncGetUserInfo(final String username, final EMValueCallBack<DlUser> callback){
        ParseQuery<ParseObject> pQuery = ParseQuery.getQuery(CONFIG_TABLE_NAME);
        pQuery.whereEqualTo(CONFIG_USERNAME, username);
        pQuery.getFirstInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject pUser, ParseException e) {
                if (pUser != null) {
                    String nick = pUser.getString(CONFIG_NICK);
                    ParseFile pFile = pUser.getParseFile(CONFIG_AVATAR);
                    if (callback != null) {
                        DlUser user = new DlUser(username);
                        user.setNick(nick);
                        if (pFile != null && pFile.getUrl() != null) {
                            user.setAvatar(pFile.getUrl());
                        }
                        callback.onSuccess(user);
                    }
                } else {
                    if (callback != null) {
                        callback.onError(e.getCode(), e.getMessage());
                    }
                }

            }
        });
    }

    /**
     * 是否登录成功过
     *
     * @return
     */
    public boolean isLoggedIn() {
        return EMChat.getInstance().isLoggedIn();
    }

    public static synchronized DlUser getCurrentUserInfo() {
        if (currentUser == null) {
            String username = EMChatManager.getInstance().getCurrentUser();
            currentUser = new DlUser(username);
            String nick = getThreeCurrentUserNick();
            currentUser.setNick((nick != null) ? nick : username);
            currentUser.setAvatar(getThreeCurrentUserAvatar());
        }
        return currentUser;
    }





    private static void setThreeCurrentUserNick(String nickname) {
        getCurrentUserInfo().setNick(nickname);
        PreferenceHelper.getInstance().setThreeCurrentUserNick(nickname);
    }

    private static void setThreeCurrentUserAvatar(String avatar) {
        getCurrentUserInfo().setAvatar(avatar);
        PreferenceHelper.getInstance().setThreeCurrentUserAvatar(avatar);
    }

    private static String getThreeCurrentUserNick() {
        return PreferenceHelper.getInstance().getThreeCurrentUserNick();
    }

    private static String getThreeCurrentUserAvatar() {
        return PreferenceHelper.getInstance().getThreeCurrentUserAvatar();
    }


}
